/*
 * @Author: wangjh wangjiahui0710@163.com
 * @Date: 2024-07-23 09:30:02
 * @LastEditors: wangjh wangjiahui0710@163.com
 * @LastEditTime: 2024-09-27 19:28:07
 * @FilePath: \fbhgl_client\src\stores\user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { apiLogin } from "@/genapi/login";
import type { components } from "@/genapi/wh";
import { jeecgBootSysUserSelfInfoUsingGet } from "@/genapi/system";
import { jeecgBootSysLoginV2UsingPost } from "@/genapi/system";
import * as Cesium from "cesium";
import { useSessionStorage } from "@vueuse/core";

export const useUserStore = defineStore("user", () => {
	// token
	const authorization = useSessionStorage(
    
		"authorization",
    sessionStorage.getItem("authorization") || "",
	);
	const router = useRouter();

	// 当前是否已登录
	const isLogined = computed(() => {
		return !!authorization.value;
	});

	const login = async (username: string, password: string) => {
		const res = await jeecgBootSysLoginV2UsingPost({
			data: { username: username, password: password },
		});
		authorization.value = res.result?.token ?? "";
	};

	const logout = () => {
		authorization.value = "";
		router.replace({ name: "LoginPage" });
	};

	const userInfo = shallowRef<
		components["schemas"]["LoginUserInfoDTO"] & Record<string, any>
	>();

	/**
	 *  用户类型：1-企业用户 2-园区用户 3-第三方单位ID
	 */
	const userType = computed<"admin" | "enterprise" | "cadets" | undefined>(
		() => {
			// if (userInfo.value?.realname === "管理员") {
			// 	return "admin";
			// }
			const type = userInfo.value?.role;
			return type?.toString() as "admin" | "enterprise" | "cadets" | undefined; // TODO 用户类型判断暂时去掉
			// return "admin";
		},
	);

	const getUserInfo = async () => {
		const { result = {} } = await jeecgBootSysUserSelfInfoUsingGet({});
		userInfo.value = result;
	};

	watchImmediate(authorization, (authorization) => {
		if (authorization) {
			getUserInfo();
		} else {
			userInfo.value = {};
			logout();
		}
	});
	const imgType = ref("");
	const applicationPersonImage = ref("");
	const applicationPersonnumUpImage = ref("");
	const dangerEnterpriseBusinessCertificate = ref("");
	const dangerRoadCertificate = ref("");
	const driveCertificate = ref("");
	const driverPersonNumberUp = ref("");
	const driverPersonNumberDown = ref("");
	const driverQualifications = ref("");
	const escortsQualifications = ref("");
	const escortsNumUp = ref("");
	const escortsNumDown = ref("");
	const drivingLicense = ref("");
	const transportCertificate = ref("");
	const applicationPersonnumDownImage = ref("");
	const insuranceId = ref("");

	const viewer = ref<any>();

	return {
		authorization,
		isLogined,
		login,
		logout,
		userInfo,
		userType,
		getUserInfo,
		imgType,
		applicationPersonImage,
		applicationPersonnumUpImage,
		applicationPersonnumDownImage,
		dangerEnterpriseBusinessCertificate,
		dangerRoadCertificate,
		driveCertificate,
		driverPersonNumberUp,
		driverPersonNumberDown,
		driverQualifications,
		escortsQualifications,
		escortsNumUp,
		escortsNumDown,
		drivingLicense,
		transportCertificate,
		insuranceId,
		viewer,
	};
});
